<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class WatchList extends MY_Controller
{
    public function __construct(){
        parent::__construct();
        $this->load->library('calculatordata');
        $this->load->model('repos/watchlistmodel', 'WLModel');
    }
    
    public function index()
    {
        $data['arrWatchList'] = $this->WLModel->getWatchLists();
        $this->renderView('watchlist/index', $data);
    }

    public function calculatewatchlist()
    {
        //check if is post data
        if (!$this->input->post()) {
            show_404();
        }

        //Get watch list id
        $wlId = $this->input->post('id', true);
        //set date for get data
        $startDate = date('Y-m-d H:i:s', strtotime("- 1 year"));
        $startDate = new DateTime($startDate, new DateTimeZone(TIMEZONE));
        $endDate = new DateTime('now', new DateTimeZone(TIMEZONE));

        $this->load->model("repos/symbolmodel", "SymbolModel");
        $arrWlDetail = $this->WLModel->loadWatchListDetail($wlId);
        $arrSymbol = $this->SymbolModel->getSymbol();
        $data['message'] = "";
        $data['arrCmp'] = array();

        foreach ($arrWlDetail as $detail) {
            $cmpA = $detail->getSymbolIdA();
            $cmpB = $detail->getSymbolIdB();
            $companyA = $this->getUrlValFromId($arrSymbol, $cmpA);
            $companyB = $this->getUrlValFromId($arrSymbol, $cmpB);
            $arrCmpA = $this->SymbolModel->getSymbolData($startDate, $endDate, $cmpA);
            $arrCmpB = $this->SymbolModel->getSymbolData($startDate, $endDate, $cmpB);
            if (empty($arrCmpA) || empty($arrCmpB)) {
                $data['message'] .= "<span class='error'>" . NO_RECORD_FOUND . " for <strong>{$companyA}</strong> and <strong>{$companyB}</strong> 
                    from <strong>{$startDate->format('d-M-Y')}</strong> to <strong>{$endDate->format('d-M-Y')}</strong> !</span><br />";
            } else {
                $arrCmp = $this->calculatordata->getDataOutPut($arrCmpA, $arrCmpB, $companyA, $companyB);
                //check return data, if has data, only get first row
                if (count($arrCmp) > 0) {
                    $cmpInfo['banks'] = array('cmpa' => $companyA, 'cmpb' => $companyB);
                    $cmpInfo['datacmp'] = $arrCmp[0];
                    array_push($data['arrCmp'], $cmpInfo);
                } else {
                    $data['message'] .= "<span class='error'> " . NO_RECORD_FOUND . " !</span>";
                }
            }
        }

        $this->load->view('watchlist/table', $data);
    }

    /**
     * Add watch list
     **/
    public function addwatchlist()
    {
        if (!$this->input->post()) {
            show_404();
        }
        $wlName = $this->input->post('watchlistname', true);
        $wlName = trim($wlName);
        if (strlen($wlName) == 0) {
            echo "<span class='error'>Watch list name invalid !</span>";
            return;
        }

        $wl = $this->input->post('cmp');
        if (empty($wl)) {
            echo "<span class='error'>Watch list empty !</span>";
            return;
        }

        //create array wldetails entity
        $arrWLDetailse = array();
        $message = "";

        foreach ($wl as $p) {
            if ($p[0] != -1 && $p[1] != -1 && $p[0] != $p[1]) {
                $pairs = new WatchListDetailsEntity();
                $pairs->setSymbolIdA($p[0]);
                $pairs->setSymbolIdB($p[1]);
                array_push($arrWLDetailse, $pairs);
            } else {
                if ($p[0] == -1 && $p[1] == -1) {
                    if ($message != "Some pairs of banks was the same !") {
                        $message = "Watch list empty !";
                    }
                }
                if ($p[0] == $p[1] && $p[0] != -1) {
                    $message = "Some pairs of banks was the same !";
                }
            }
        }

        if (empty($arrWLDetailse)) {
            echo "<span class='error'>$message</span>";
            return;
        }

        //remove duplicate pairs
        $arrWLDetailse = array_unique($arrWLDetailse, SORT_REGULAR);

        $this->load->model('repos/watchlistmodel', 'WLModel');
        $result = $this->WLModel->insertWatchList($wlName, $arrWLDetailse);
        if ($result < 1) {
            $message = "<span class='error'>Fail ! Watch list already existed</span>";
        } else {
            $message = "<span class='success'>Add watch list successful !</span>";
        }
        echo $message;
        return;
    }

    public function reloadwatchlist()
    {
        $this->load->model('repos/watchlistmodel', 'WLModel');
        $data['arrWatchList'] = $this->WLModel->getWatchLists();
        $this->load->view('manage/watchlist/watchlist', $data);
    }

    public function update()
    {
        $id = $this->input->post('id', true);
        if (is_null($id) || !is_numeric($id)) {
            show_404();
        }
        $this->load->model('repos/symbolmodel', 'SymbolModel');
        $this->load->model('repos/watchlistmodel', 'WLModel');

        $data['arrSymbol'] = $this->SymbolModel->getSymbol();
        $data['arrSelect'] = array();
        $data['max1'] = 10;
        $data['max2'] = 30;
        $arrSelect = $this->WLModel->loadWatchListDetail($id);
        foreach ($arrSelect as $item) {
            $arr = array();
            $arr[0] = $item->getSymbolIdA();
            $arr[1] = $item->getSymbolIdB();
            array_push($data['arrSelect'], $arr);
        }
        $this->load->view('manage/watchlist/pairslist', $data);
    }

    public function delete()
    {
        $id = $this->input->post('id', true);
        if (is_null($id) || !is_numeric($id)) {
            show_404();
        }

        $this->load->model('repos/watchlistmodel', 'WLModel');
        $result = $this->WLModel->deleteWL($id);
        $mess = "<span class='error'>Delete watchlist successful !</span>";
        if ($result > 0) {
            $mess = "<span class='success'>Delete watchlist successful !</span>";
        }

        echo $mess;
        return;
    }

    public function doupdatewatchlist()
    {
        if (!$this->input->post()) {
            show_404();
        }
        $id = $this->input->post('id', true);
        $id = trim($id);
        if (is_null($id) || !is_numeric($id)) {
            echo "<span class='error'>Wrong infomation !</span>";
            return;
        }
        $wlName = $this->input->post('watchlistname', true);        
        $wlName = trim($wlName);
        if (strlen($wlName) == 0) {
            echo "<span class='error'>Watch list name invalid !</span>";
            return;
        }

        $wl = $this->input->post('cmp');
        if (empty($wl)) {
            echo "<span class='error'>Watch list empty !</span>";
            return;
        }

		$this->load->model('repos/watchlistmodel', 'WLModel');
		
        //create array wldetails entity
        $arrWLDetailse = array();
        $message = "";
        foreach ($wl as $p) {
            if ($p[0] != -1 && $p[1] != -1 && $p[0] != $p[1]) {
                $pairs = new WatchListDetailsEntity();
                $pairs->setSymbolIdA($p[0]);
                $pairs->setSymbolIdB($p[1]);
                array_push($arrWLDetailse, $pairs);
            } else {
                if ($p[0] == -1 && $p[1] == -1) {
                    if ($message != "Some pairs of banks was the same !") {
                        $message = "Watch list empty !";
                    }
                }
                if ($p[0] == $p[1] && $p[0] != -1) {
                    $message = "Some pairs of banks was the same !";
                }
            }
        }

        if (empty($arrWLDetailse)) {
            echo "<span class='error'>$message</span>";
            return;
        }

        //remove duplicate pairs
        $arrWLDetailse = array_unique($arrWLDetailse, SORT_REGULAR);

        //new wl attr
        $wlInfo = new WatchListEntity();
        $wlInfo->setId($id);
        $wlInfo->setName($wlName);
        $result = $this->WLModel->updateWatchList($wlInfo, $arrWLDetailse);
        if ($result < 1) {
            $message = "<span class='error'>Update watch list fail !</span>";
        } else {
            $message = "<span class='success'>Update watch list successful !</span>";
        }
        echo $message;
        return;
    }
}
?>